import pymysql


class Conn:

    def __init__(self, config):
        self.conf = config
        self.conn = pymysql.Connect(**self.conf)
        self.cursor = self.conn.cursor(pymysql.cursors.DictCursor)

    def execute_sql(self, sql: str, count='all'):

        self.cursor.execute(sql)
        method = sql.strip()[:6].upper()
        if method == 'SELECT':
            if count == 'one':
                data = self.cursor.fetchone()
            else:
                data = self.cursor.fetchall()
        else:
            data = self.cursor.rowcount
            self.conn.commit()
        return data

if __name__ == '__main__':
    config = {
        "user": 'root',  # 用户名
        "password": "mmvk123456",  # 密码
        "host": '10.35.161.54',  # 主机地址 - localhost
        "database": "tpshop",  # 数据库名
        "port": 3306,  # 端口号
        "charset": "utf8",  # 编码格式
    }
    conn = Conn(config)
    data = conn.execute_sql("SELECT * FROM tp_users WHERE mobile = '13800138006'", count='one')
    print(data)
